//
//  MealDetailViewController.m
//  Restaurant
//
//  Created by wanghuan on 15/4/22.
//  Copyright (c) 2015年 wanghuan. All rights reserved.
//

#import "MealDetailViewController.h"

@interface MealDetailViewController () {

    UIButton *_shopNumBtn;

}

@end

@implementation MealDetailViewController

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    self.tabBarController.tabBar.hidden = YES;
    
    
}




- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.view.backgroundColor = AppBackGroundColor;
    
    
    self.NaTitltLabel.text = self.mealModel.title;
    self.cellTitle.text = self.mealModel.title;
    self.cellPricesLabel.text = [NSString stringWithFormat:@"￥%@",self.mealModel.marketprice];
    [self.cellImageView sd_setImageWithURL:[NSURL URLWithString:self.mealModel.productImg] placeholderImage:nil];
    
    _shopTableView = [[ShopTableView alloc]initWithFrame:CGRectMake(0, APPHeight, APPWith, 3*50+40)];
    
    _shopTableView.shopBtnSelected = self.shopBtn;
    
    _shopTableView.shopArr = _shopArr;
    
    self.priceLabel.text = self.priceStr;
    
    self.certainBtn.layer.cornerRadius = 3;
    
    if (_shopArr.count) {
        self.certainBtn.backgroundColor = [UIColor colorWithRed:231.0/255.0 green:67.0/255.0 blue:73.0/255.0 alpha:1];
    }else {
    
        self.certainBtn.backgroundColor = [UIColor grayColor];
    }
    
    [self.view bringSubviewToFront:self.bottomView];
    
    [self.view insertSubview:_shopTableView belowSubview:_bottomView];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(ShopframeChange:) name:@"ShopframeChange" object:nil];
    
    _shopTableView.AddBlock = ^(UIButton *sender) {
        
        [self addClick:sender];
        
    };
    
    _shopTableView.DelegateBlock = ^(UIButton *sender) {
        
        [self deleteClick:sender];
        
    };
    
    
    [self createShopNumBtn];
}

#pragma mark - 创建购物车的商品个数的按钮

- (void)createShopNumBtn {
    
    //创建购物车的商品个数的按钮
    
    _shopNumBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    _shopNumBtn.frame = CGRectMake(20, -5, 21, 21);
    
    [_shopNumBtn setTitle:[NSString stringWithFormat:@"%lu",(unsigned long)[_shopArr count]] forState:UIControlStateNormal];
    
    _shopNumBtn.titleLabel.font = [UIFont boldSystemFontOfSize:11];
    
    [_shopNumBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    [_shopNumBtn setBackgroundImage:[UIImage imageNamed:@"k1"] forState:UIControlStateNormal];
    
    [self.shopBtn addSubview:_shopNumBtn];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 增加选菜的数量
- (void)deleteClick:(UIButton *)sender {
    
    
    int number = [[_numberArr objectAtIndex:sender.tag] intValue];
    
    NSString *numStr = [_numberArr objectAtIndex:sender.tag];
    
    if (--number < 0) {
        return;
    }
    
    numStr = [NSString stringWithFormat:@"%d",number];
    [_numberArr replaceObjectAtIndex:sender.tag withObject:numStr];
    
    [self addModel:sender];
    
}

- (void)addClick:(UIButton *)sender {
    
    int number = [[_numberArr objectAtIndex:sender.tag] intValue];
    
    NSString *numStr = [_numberArr objectAtIndex:sender.tag];
    
    
    if (++number > 20) {
        return;
    }
    
    numStr = [NSString stringWithFormat:@"%d",number];
    
    [_numberArr replaceObjectAtIndex:sender.tag withObject:numStr];
    
    [self addModel:sender];
    
}

#pragma mark - 改变选好了按钮的背景色

- (void)certainBtnbackgroundColor {

    if (![self.shopArr count]) {
        
        self.certainBtn.backgroundColor = [UIColor grayColor];
        
    } else {
        
        self.certainBtn.backgroundColor = [UIColor colorWithRed:231.0/255.0 green:67.0/255.0 blue:73.0/255.0 alpha:1];
        
    }

}

//在添加、删除数据
- (void)addModel:(UIButton *)sender {
    
    if (self.shopBtn.selected) {
        
        
        if (_shopArr.count > 2) {
            _shopTableView.frame = CGRectMake(0, APPHeight-3*50-40-49, APPWith,3*50-40);
        } else {
            
            _shopTableView.frame = CGRectMake(0, APPHeight-_shopArr.count*50-40-49, APPWith,_shopArr.count*50-40);
            
        }
    }

    
    float price = 0;
    
    NSString *numStr = [_numberArr objectAtIndex:sender.tag];
    
    MealTableModel *model = [_tableArr objectAtIndex:sender.tag];
    model.amount = numStr;
    
    for (shoppingModel *Smodel in _shopArr) {
        if ([Smodel.productId isEqual:model.productId]) {
            
            if (![numStr isEqual:@"0"]) {
                Smodel.number = model.amount;
                
                
            } else {
                
                [_shopArr removeObject:Smodel];
            }
            _shopTableView.shopArr = _shopArr;
            
            [_shopTableView.cellArray removeAllObjects];
            
            [_shopNumBtn setTitle:[NSString stringWithFormat:@"%lu",(unsigned long)_shopArr.count] forState:UIControlStateNormal];
            
            [self certainBtnbackgroundColor];
            
            [_shopTableView reloadData];
           
            
            for (int i = 0; i < _shopArr.count; i++) {
                shoppingModel *sm = [_shopArr objectAtIndex:i];
                
                price += [sm.prices floatValue]*[sm.number intValue];
                
            }
            
            self.priceLabel.text = [NSString stringWithFormat:@"%.1f",price];
            
            if ([self.priceLabel.text isEqual:@"0.0"]) {
                self.priceLabel.text = @"0";
            }
            
            return;
            
        }
    }
    
    shoppingModel *Smodel = [[shoppingModel alloc]init];
    
    Smodel.nameStr = model.title;
    Smodel.prices = model.marketprice;
    Smodel.number = model.amount;
    Smodel.productId = model.productId;
    Smodel.tag = model.tag;
    [_shopArr addObject:Smodel];
    
    _shopTableView.shopArr = _shopArr;
    
    for (int i = 0; i < _shopArr.count; i++) {
        shoppingModel *sm = [_shopArr objectAtIndex:i];
        
        price += [sm.prices floatValue]*[sm.number intValue];
        
    }
    
    self.priceLabel.text = [NSString stringWithFormat:@"%.1f",price];
    
    if ([self.priceLabel.text isEqual:@"0.0"]) {
        self.priceLabel.text = @"0";
    }
    
    
    [_shopTableView.cellArray removeAllObjects];
    
    [_shopNumBtn setTitle:[NSString stringWithFormat:@"%lu",(unsigned long)_shopArr.count] forState:UIControlStateNormal];
    
    [self certainBtnbackgroundColor];
    
    [_shopTableView reloadData];
    
    
    
    
}


#pragma mark - _shopTableView的frame发生改变时
- (void)ShopframeChange:(NSNotification *)not {
    
    float price = 0;
    UIButton *btn = (UIButton *)not.object;
    if (btn.tag == 1) {
        [UIView animateWithDuration:1 animations:^{
            self.shopBtn.alpha = 1.0;
        }];
        return;
    }
    
    for (shoppingModel *Smodel in _shopTableView.DeleteArray) {
        
        int index = 0;
        
        for (MealTableModel *model in _tableArr) {
            
            if ([Smodel.productId isEqual:model.productId]) {
                model.amount = @"0";
                
                NSString *numStr = [_numberArr objectAtIndex:index];
                numStr = @"0";
                [_numberArr replaceObjectAtIndex:index withObject:numStr];
                
                index++;
            }
        }
    }
    for (int i = 0; i < _shopArr.count; i++) {
        shoppingModel *sm = [_shopArr objectAtIndex:i];
        
        price += [sm.prices floatValue]*[sm.number intValue];
        
    }
    
    self.priceLabel.text = [NSString stringWithFormat:@"%.1f",price];
    
    if ([self.priceLabel.text isEqual:@"0.0"]) {
        self.priceLabel.text = @"0";
    }
    
}

- (IBAction)backBtn:(UIButton *)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
    
    
    [[NSNotificationCenter defaultCenter]postNotificationName:@"pleasereloadData" object:nil];
}

- (IBAction)shopClick:(UIButton *)sender {
    
    self.shopBtn.selected = !self.shopBtn.selected;
    
    [UIView animateWithDuration:1 animations:^{
        self.shopBtn.alpha = 0.0;
        if (_shopArr.count > 2) {
            _shopTableView.frame = CGRectMake(0, APPHeight-190-49, APPWith,190);
        } else {
            
            _shopTableView.frame = CGRectMake(0, APPHeight-_shopArr.count*50-40-49, APPWith,_shopArr.count*50+40);
            
        }
    }];

    
    
}
- (IBAction)certainClick:(UIButton *)sender {
    
    if (_shopArr.count) {
        OrdersViewController *ovc = [[OrdersViewController alloc]init];
        
        ovc.shopMutArray = _shopArr;
        
        ovc.price = self.priceLabel.text;
        
        ovc.tabBarController.tabBar.hidden = YES;
        [self.navigationController pushViewController:ovc animated:YES];
    }
    
}
- (IBAction)AsetClick:(UIButton *)sender {

    sender.tag = [_tableArr indexOfObject:self.mealModel];
    [self addClick:sender];
    
}
@end
